package com.huibao.hulibao.receiver;

import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.google.gson.Gson;
import com.huibao.hulibao.activity.LoginActivity;
import com.huibao.hulibao.activity.MainActivity;
import com.huibao.hulibao.activity.SplashActivity;
import com.huibao.hulibao.utils.MyUtils;
import com.huibao.hulibao.utils.T;

import cn.jpush.android.api.JPushInterface;

/**
 * 接受极光推送的消息处理
 *
 * @author lizhaoyong
 */
public class MyJPushReceiver extends BroadcastReceiver {

    public Gson gson;
    // 用户点击通知后，具体要跳转到哪个界面的标识
    public int type = 0;

    public String web = "";

    public void startAPP(String appPackageName, Context context, int type) {
        try {
            Intent intent = context.getPackageManager()
                    .getLaunchIntentForPackage(appPackageName);
            intent.putExtra("position", type);
            context.startActivity(intent);
        } catch (Exception e) {

        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        gson = new Gson();
        Bundle bundle = intent.getExtras();
        // String title = bundle
        // .getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        String json = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String content = bundle.getString(JPushInterface.EXTRA_ALERT);
        if (content != null && !content.equals("")) {
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone rt = RingtoneManager.getRingtone(context, uri);
            rt.play();
            // T.show(context, content, 0);
            Toast toast = Toast.makeText(context, "" + content, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.TOP, 0, 40);
            toast.show();
        }
        // LogUtils.d("msg = " + content);
        // T.show(context, content, 0);
        // LogUtils.d("json = " + json);
        // String json = "{\"_info_\":\"web\",\"type\":\"CheckPush\"}";
        if (json != null) {
            if (json.contains("\"type\":\"msg\"")) {
                type = 1;
                web = "";
            } else if (json.contains("\"type\":\"task\"")) {
                type = 2;
                web = "";
            } else if (json.contains("web")) {
                type = 3;
                web = json;
            } else {
                web = "";
            }
        }

        // 用户点击了通知
        if (JPushInterface.ACTION_NOTIFICATION_OPENED
                .equals(intent.getAction())) {
            if (MyUtils.appIsRunning(context)) {
                Intent intent_main = new Intent(context, MainActivity.class);
                intent_main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                switch (type) {
                    case 1:
                        intent_main.putExtra("position", 1);
                        context.startActivity(intent_main);
                        // LogUtils.d("1");
                        break;
                    case 2:
                        intent_main.putExtra("position", 2);
                        // LogUtils.d("2");
                        context.startActivity(intent_main);
                        break;
                    case 3:
                        // 跳转到web页面
                        intent_main.putExtra("position", 3);
                        intent_main.putExtra("web", web);
                        context.startActivity(intent_main);
                        break;
                }
            } else {
                startAPP("com.huibao.hulibao", context, type);
            }
        }
    }
}
